package cn.jcby.app.lebu.utils;

import org.apache.commons.lang.math.RandomUtils;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author: lihaotian
 * @since: 2023/7/26 17:32
 * @description
 **/
public class Constants {

    //星座
    public static final String CONSTELLATION = "SYS_14" ;
    //职业
    public static final String OCCUPATION = "SYS_15";
    //标签
    public static final String TAG_LIST = "SYS_17";
    public static final String MOTION_FREQUENCY = "SYS_20";

    public static final String BASE_CHAR = "1234567890abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";

    private static final int EARTH_RADIUS = 6371000; // 地球平均半径（单位：米）


    /**
     * 将
     * @param mobile
     * @return
     */
    public static String getInviteCode(String mobile){
        StringBuffer sb = new StringBuffer();
        mobile = mobile+RandomUtils.nextInt(10) ;
        System.out.println(mobile);
        for(int i=0;i<6;i++){
            int start = i*2 ;
            int end = (i+1)*2 ;
            String sub = mobile.substring(start,end);
            int index = Integer.valueOf(sub)%BASE_CHAR.length();
            char c = BASE_CHAR.charAt(index);
            sb.append(c) ;
        }
        return sb.toString();
    }
    /**
     *
     * @param latitude 维度
     * @param longitude 经度
     * @param latitude1 维度
     * @param longitude1 经度
     * @return
     */
    public static String getDistance(double latitude, double longitude, double latitude1, double longitude1) {
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(
                Ellipsoid.Sphere,
                new GlobalCoordinates(latitude, longitude),
                new GlobalCoordinates(latitude1, longitude1)
        );
        double ellipsoidalDistance = geoCurve.getEllipsoidalDistance();
        return ellipsoidalDistance>1000?
                BigDecimal.valueOf(ellipsoidalDistance).divide(BigDecimal.valueOf(1000),2,RoundingMode.HALF_UP).toPlainString()+"km":
                BigDecimal.valueOf(ellipsoidalDistance).setScale(2, RoundingMode.HALF_UP).toPlainString()+"m" ;
    }


    public static String getMileageStr(BigDecimal mileage){
        if (mileage.compareTo(BigDecimal.valueOf(1000)) >= 0){
            if(mileage.longValue()%1000==0){
                return (mileage.longValue() / 1000) + "公里";
            }else {
                return mileage.divide(new BigDecimal(1000), 2, RoundingMode.HALF_UP).toPlainString() + "公里";
            }
        }else{
            return mileage.toPlainString() + "米";
        }
    }

    public static String getMileageStrEn(BigDecimal mileage){
        if (mileage.compareTo(BigDecimal.valueOf(1000)) >= 0){
            if(mileage.longValue()%1000==0){
                return (mileage.longValue() / 1000) + "km";
            }else {
                return mileage.divide(new BigDecimal(1000), 2, RoundingMode.HALF_UP).toPlainString() + "km";
            }
        }else{
            return mileage.toPlainString() + "m";
        }
    }

    public static boolean isNumeric(String str) {
        return str.matches("\\d+");
    }

    public static void main(String[] args) {
        double lat1 = 22.678611;
        double lon1 = 113.805695;
        double lat2 = 22.716473;
        double lon2 = 113.826391;
        System.out.println(getDistance(lat1,lon1,lat2,lon2));
    }
}
